<template>
<div>
  <div class="container-body">
    <div class="main-body-swap">
      <div class="main-body-header-nav">
        <span class="active span-margin">推荐</span>
        <span class="span-margin">关注</span>
        <span class="span-margin">热榜</span>
      </div>

      <div class="main-body" >
            <div class="card-item-body" v-for="(workInfo,index) in workInfoList" @click="lookWorkInfo(workInfo.uuid)" :key="index">
              <div class="card-content">
                <div class="card-item">
                  <span class="card-title-font" >{{index+1}}. {{workInfo.title}}</span>
                </div>
                <span class="card-content-body" v-html="workInfo.content"></span>
              </div>
              <div class="card-footer">
                <span>关注({{workInfo.followNum}})</span>
                <span>回答({{workInfo.commentNum}})</span>
              </div>
            </div>

            <div class="load-more" v-show="!queryFlag">
              <span>没有更多了</span>
            </div>
      </div>
    </div>
    <div class="rightInfo">
      <div class="global-writer">
        <div class="global-writer-info">
              <a class="title" @click="writeArticle()">写文章</a>
              <a class="title" @click="writeProblem()">写问题</a>
              <a class="title">写回答</a>
        </div>
      </div>
    </div>
  </div>
</div>
</template>
<script>

    export default {
      components: {
      },
      data(){
        return {
          workInfoList:[],
          searchContent:"",
          pageSize: 8,
          pageNum: 0,
          isClearData:false,
          queryFlag: true//如果返回数据为空的话,就把这个参数设置为false,避免重复请求数据
        }
      },
      name: "searchInfo",
      created:function(){    //creadted 实例创建完成之后立即调用,此时实例已经完成了:数据观测,属性和方法的运算.watch和event事件的回调,挂载阶段没有开始;因此,该函数一般是用来初始化数据的 ,html渲染完成之后才会调用mounted这个函数,dom操作一般在mounted函数中执行

        if(this.queryFlag === true){
          this.queryWorkInfo();
        }
      },
      methods:{

        handleScroll:function () {

          const _scrollTop = document.documentElement.scrollTop;//可滚动容器超出当前窗口显示范围的高度
          const clientHeight = document.documentElement.clientHeight;//屏幕的尺寸高度
          const contentHeight = document.body.offsetHeight-5;//可滚动屏幕大小

            if (_scrollTop + clientHeight >= contentHeight){//加载更多
               this.isClearData = false;
               this.queryWorkInfo();
            }
          },
        queryWorkInfo:function () {

          if (this.queryFlag === false){
            return
          }

          this.pageNum = this.pageNum + 1;
          this.$axios({
            method: 'post',
            url: '/solvePlat/workInfo/selectWorkInfo',
            data: {
              pageSize: this.pageSize,
              pageNum: this.pageNum,
              content: this.searchContent
            }
          })
            .then(res => {
              if (res.data.success === true){//成功
                if (res.data.data.list.length != 0){
                  if (this.isClearData){
                    this.workInfoList = [];
                  }
                  this.workInfoList.push.apply(this.workInfoList,res.data.data.list)
                }
                else{
                  this.queryFlag = false;
                }
              }
            })
            .catch(err => {
              console.log('err', err)
            })
        },
        writeArticle:function () {
          let writeArticle = this.$router.resolve({
            name: 'writeArticle'
          })
          window.open(writeArticle.href, '_blank');
        },
        writeProblem:function () {
          let writeProblem = this.$router.resolve({
            name: 'writeProblem'
          })
          window.open(writeProblem.href, '_blank');
        },
        lookWorkInfo:function (uuid) {//查看当前任务详情信息
          this.addWorkTaskLookNum(uuid);
          this.$router.push({
                            name:"ProblemInfo",
                            query:{
                              uuid:uuid
                            }
          })
        },
        addWorkTaskLookNum:function (workInfoId) {

          this.$axios({
            method: 'get',
            url: '/solvePlat/workInfo/addBrowseNum?uuid='+workInfoId,
          })
            .then(res => {
              if (res.data.success === true){//成功
                if (res.data.data.list.length != 0){
                }
                else{
                }
              }
            })
            .catch(err => {
              console.log('err', err)
            })
        }
      },
      mounted:function () {
        window.addEventListener('scroll', this.handleScroll)
      }
    }
</script>



<style scoped>

  .load-more{
    text-align: center;
    width: 100%;
    font-size: 15px;
    font-weight: 600;
  }

  .rightInfo{
    display: inline-block;
    background-color: #f6f6f6;
    margin-left: 15px;
    width: 15%;
    position: sticky;
    top: 74px;
    vertical-align: top;
  }

.global-writer-info{
  display: flex;
  justify-content: space-around;
}

  .global-writer{
    background-color: white;
    height: 50px;
    padding-top: 40px;
  }








  .global-writer-info .title{
    cursor: pointer;
    font-weight: 100;
    font-size: 18px;
  }
  
  .main-body{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding-right: 10px;
    padding-bottom: -10px;
  }

  .main-body-swap{
    display: inline-block;
    width: 60%;
    background-color: white;
    padding: 10px 10px;
    margin-left: 10%;
    margin-top: 10px;
  }


  .card-item-body{
    display: inline-block;
    padding: 10px 10px;
    border: solid 1px #eeeeee;
    margin-left: 10px;
    margin-bottom: 10px;
    width: 100%;
    height:  96px;
  }
  .card-item-body:hover{
   box-shadow: 0px 0px 8px #cccccc;
    cursor: pointer;
  }
  .card-title-font{
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
    vertical-align: middle;
  }
  .card-content-body{
    display: block;
    height: 54px;
    margin-top: 10px;
    width: 80%;
  }

  .main-body-header-nav{
    margin-left: 12px;
    margin-top: 15px;
    margin-bottom: 14px;
    font-size: 15px;
    font-weight: 600;
    border-bottom: 1px solid #dccce5a6;
    padding-bottom: 10px;
  }

  .active{
    color: #0084ff;
  }

  .main-body-header-nav .span-margin{
    margin-right: 10px;
  }
  .card-item-status-unfindish{
    background-color: #f90;
    color: white;
    padding: 6px 8px;
    vertical-align: middle;
  }
  .card-item{
    display: inline-block;
    vertical-align: text-bottom;
  }
  .card-item-status{
    display: inline-block;
    vertical-align: text-bottom;
    padding-left: 20px;
  }

  .container-body{
    background-color: #f6f6f6;
    min-height: 790px;
  }
</style>
